//
//  TKGroupPhotoViewController.m
//  TKPhotoSelectDemo
//
//  Created by liupm on 16/3/14.
//  Copyright © 2016年 liupm. All rights reserved.
//

#import "TKIMImageSelectViewController.h"
#import "TKIMImageSelectView.h"
#import "TKIMPhotoSelectHelper.h"
#import "TKIMMulImageBrowserViewController.h"
#import "TKIMImagePickerDefinition.h"

@interface TKIMImageSelectViewController ()
@property(nonatomic,strong) TKIMPhotoSelectHelper *phtoHelper;
//图片选择视图
@property(nonatomic,strong) TKIMImageSelectView *imageSelectView;
@property(nonatomic,strong) NSMutableArray *dataSource;
@end
@implementation TKIMImageSelectViewController

-(void)loadView
{
    [super loadView];
    self.navigationItem.title = _model.albumName;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(close)];

    self.view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.imageSelectView];
    __weak typeof(self) weakSelf = self;
    [self.phtoHelper getAllPhotos:_model callBack:^(NSMutableArray *array) {
        _dataSource = array;
        [weakSelf.imageSelectView reloadData:_dataSource];
    }];
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.imageSelectView reloadView];
}


/**
 *  @Author lpm, 16-04-21 16:04:33
 *
 *  @brief  关闭当前控制器
 */
-(void)close
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


/**
 *  @Author lpm, 16-04-21 13:04:24
 *
 *  @brief  事件路由
 *
 *  @param eventName
 *  @param userInfo
 */
-(void)routerEventWithName:(NSString *)eventName userInfo:(NSObject *)userInfo
{
    if([eventName isEqualToString:TKIMImagePreviewEvent])
    {
        TKIMMulImageBrowserViewController *browerCtrl = [[TKIMMulImageBrowserViewController alloc] init];
        browerCtrl.selectedArray = (NSMutableArray *)userInfo;
        browerCtrl.model = _model;
        [self.navigationController pushViewController:browerCtrl animated:YES];
    }
    else if([eventName isEqualToString:TKIMImageConfirmEvent])
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:TKIMImagePickerCloseEvent object:(NSMutableArray *)userInfo];
    }
    else
    {
        [super routerEventWithName:eventName userInfo:userInfo];
    }
}


#pragma mark 属性
-(TKIMPhotoSelectHelper *)phtoHelper
{
    if (!_phtoHelper)
    {
        _phtoHelper = [[TKIMPhotoSelectHelper alloc] init];
    }
    return _phtoHelper;
}

-(TKIMImageSelectView *)imageSelectView
{
    if (!_imageSelectView)
    {
        _imageSelectView =  [[TKIMImageSelectView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-NAVIGATION_BAR_HEIGHT-HOME_INDICATOR_HEIGHT)];
    }
    return _imageSelectView;
}

@end
